package com.abel.bigwater.data.jnrq

import com.abel.bigwater.data.BwDataAnjiApplication
import com.abel.bigwater.data.MsgHelper
import com.alibaba.fastjson.JSON
import io.netty.channel.embedded.EmbeddedChannel
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import java.io.FileWriter

@SpringBootTest
@RunWith(SpringRunner::class)
//@ActiveProfiles("test")
class RtuMeterCollectionTests {
    companion object {
        private val lgr = LoggerFactory.getLogger(RtuMeterCollectionTests::class.java)
    }

    @Autowired
    var app: BwDataAnjiApplication? = null

    @Autowired
    var msgHelper: MsgHelper? = null

    var ch: EmbeddedChannel? = null

    var tcpHandler: JnrqTcpHandler? = null

    @Before
    fun beforeTest() {
        tcpHandler = JnrqTcpHandler(
                app!!.dataMapper!!,
                app!!.rtuMapper!!,
                app!!.firmId!!,
                app!!.firmCode!!,
                msgHelper!!,
                app!!.deviceTemplate!!)
        ch = EmbeddedChannel(JnrqDecoder("11", "zn-", msgHelper), tcpHandler)
    }

    @Test
    fun testInit() {
        lgr.info("loaded:\n${JSON.toJSONString(tcpHandler?.checkDownload("53241"), true)}")
    }

    @Test
    fun testCollection() {
        val rmc = RtuMeterCollection().also {
            it.rtuMeterList = ArrayList()
            it.rtuMeterList!!.add(RtuMeter().apply {
                rtuAddrInt = 0x1234
                meterAddrList = arrayListOf("01421009001695"
                        , "01421009001696"
                        , "01421009001697"
                        , "01421009001699"
                        , "01421009001700"
                        , "01421009005101"
                        , "01421009005102"
                        , "01421009005103"
                        , "01421009005104"
                        , "01421009005105"
                        , "01421009005106"
                        , "01421009005107"
                        , "01421009005108"
                        , "01421009005109"
                        , "01421009005111"
                        , "01421009005112"
                        , "01421009005113"
                        , "01421009005114"
                        , "01421009005115"
                        , "01421009005116"
                        , "01421009005117"
                        , "01421009005118"
                        , "01421009005119"
                        , "01421009005120"
                        , "01421009005121"
                        , "01421009005122"
                        , "01421009005123"
                        , "01421009005124"
                        , "01421009005125"
                        , "01421009005126"
                        , "01421006005481"
                        , "01421009001307"
                        , "01421009001308"
                        , "01421009001312"
                        , "01421009001314"
                        , "01421009001324"
                        , "01421009001326"
                        , "01421009001327"
                        , "01421009001331"
                        , "01421009001332"
                        , "01421009001333"
                        , "01421009001335"
                        , "01421009001338"
                        , "01421009001339"
                        , "01421009001342"
                        , "01421009007410"
                        , "01421009007411"
                        , "01421009007413"
                        , "01421009007414"
                        , "01421009007415"
                        , "01421009007416"
                        , "01421009007417"
                        , "01421009007419"
                        , "01421009007420"
                        , "01421009007421"
                        , "01421009007422"
                        , "01421009007423"
                        , "01421009007424"
                        , "01421009007425"
                        , "01421009007426"
                        , "01421009005127"
                        , "01421009005128"
                        , "01421009005129"
                        , "01421009005130"
                        , "01421009005131"
                        , "01421009005132"
                        , "01421009005133"
                        , "01421009005134"
                        , "01421009005135"
                        , "01421009005136"
                        , "01421009005137"
                        , "01421009005138"
                        , "01421009005139"
                        , "01421009005140"
                        , "01421009005141"
                        , "01421009006994"
                        , "01421009006993"
                        , "01421009006997"
                        , "01421009006998"
                        , "01421009007000"
                        , "01421009006995"
                        , "01421009006999"
                        , "01421009007451"
                        , "01421009007453"
                        , "01421009007457"
                        , "01421009007454"
                        , "01421009007456"
                        , "01421009007458"
                        , "01421009007452"
                        , "01421009006054"
                        , "01421009006067"
                        , "01421009006076"
                        , "01421009006061"
                        , "01421009006084"
                        , "01421009006073"
                        , "01421009006072"
                        , "01421009006071"
                        , "01421009006062"
                        , "01421009006069"
                        , "01421009006058")
            })
        }

        FileWriter("rtu-meter.json").apply {
            try {
                write(JSON.toJSONString(rmc, true))
            } finally {
                close()
            }
        }

        lgr.info("write meter-addr to rtu.")
    }
}